using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V4.App;
using TMM.Common;

namespace TMM
{
    [Service]
    public class NotificationsIntentService : IntentService
    {
        static PowerManager.WakeLock sWakeLock;
        static object LOCK = new object();

        public static void RunIntentInService(Context context, Intent intent)
        {
            lock (LOCK)
            {
                if (sWakeLock == null)
                {
                    var pm = PowerManager.FromContext(context);
                    sWakeLock = pm.NewWakeLock(WakeLockFlags.Partial, "TMM WakeLock Tag");
                }
            }

            sWakeLock.Acquire();
            intent.SetClass(context, typeof(NotificationsIntentService));
            context.StartService(intent);
        }

        protected override void OnHandleIntent(Intent intent)
        {
            try
            {
                Context context = this.ApplicationContext;
                string action = intent.Action;

                if (action.Equals("com.google.android.c2dm.intent.REGISTRATION"))
                {
                    HandleRegistration(context, intent);
                }
                else if (action.Equals("com.google.android.c2dm.intent.RECEIVE"))
                {
                    HandleMessage(context, intent);
                }
            }
            finally
            {
                lock (LOCK)
                {
                    if (sWakeLock != null)
                        sWakeLock.Release();
                }
            }
        }

        private void HandleMessage(Context context, Intent intent)
        {
            var message = intent.GetStringExtra("message");
            var value = intent.GetStringExtra("0");
            int ccdId;
            int.TryParse(value, out ccdId);

            GenerateNotification(context, message, ccdId);
        }

        private void GenerateNotification(Context context, string message, int ccdId)
        {
            NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
            var title = "my.hrp.ro";

            Intent notificationIntent = new Intent(context, typeof(MainActivity));
            notificationIntent.SetAction("NOTIFICATION");
            notificationIntent.PutExtra("ccdId", ccdId);

            notificationIntent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
            var intent = PendingIntent.GetActivity(context, 100, notificationIntent, PendingIntentFlags.UpdateCurrent);

            var builder = new NotificationCompat.Builder(context)
                .SetAutoCancel(true)
                .SetOnlyAlertOnce(true)
                .SetSmallIcon(Resource.Drawable.ic_launcher)
                .SetContentTitle(title)
                .SetContentText(message);
            builder.SetContentIntent(intent);
            notificationManager.Notify(0, builder.Build());
        }

        private void HandleRegistration(Context context, Intent intent)
        {
            string registrationId = intent.GetStringExtra("registration_id");
            DeviceInfo.Id = registrationId;
        }
    }
}